package com.aoip.telemetry.telemetry.configureui.business.pipeline.sthandle.controller;

import com.aoip.telemetry.telemetry.configureui.business.pipeline.sthandle.service.RelStHandleService;
import com.aoip.telemetry.telemetry.configureui.common.annotation.Log;
import com.aoip.telemetry.telemetry.configureui.common.core.controller.BaseController;
import com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo;
import com.aoip.telemetry.telemetry.configureui.common.enums.BusinessType;
import com.aoip.telemetry.telemetry.core.entity.AjaxResult;
import com.aoip.telemetry.telemetry.entity.pipeline.RelStHandleEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 测站、设备关联处理器配置类（单站、区域、行业、季节、全站）
 *
 * @author chenbai
 * @version 1.0
 * @date 2022/2/14 10:46
 */
@RestController
public class RelStHandleController extends BaseController {

    @Resource
    private RelStHandleService service;

    /**
     * 测站、设备关联处理器配置分页列表
     *
     * @param entity 测站
     * @return com.aoip.telemetry.telemetry.configureui.original.core.list.TableDataInfo
     * @author chenbai
     * @date 2021/12/27
     **/
    @GetMapping("/v1/GET/st/handle/page")
    public TableDataInfo page(RelStHandleEntity entity) {
        startPage();
        List<RelStHandleEntity> list = service.list(entity);
        return getDataTable(list);
    }

    /**
     * 测站、设备关联处理器配置不分页列表
     *
     * @param entity 测站
     * @return com.aoip.telemetry.telemetry.configureui.original.core.list.TableDataInfo
     * @author chenbai
     * @date 2021/12/27
     **/
    @GetMapping("/v1/GET/st/handle/list")
    public AjaxResult list(RelStHandleEntity entity) {
        List<RelStHandleEntity> list = service.list(entity);
        return AjaxResult.success(list);
    }

    /**
     * 根据测站编码获取该测站关联的处理器策略列表
     *
     * @param stCode 测站编码
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2021/12/27
     **/
    @GetMapping("/v1/GET/st/handle/list/{stCode}")
    public AjaxResult handleList(@PathVariable String stCode) {
        return AjaxResult.success(service.getHandleListByStCode(stCode));
    }

    /**
     * 获取单个测站、设备关联处理器配置
     *
     * @param id 处理器规则主键
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2021/12/27
     **/
    @GetMapping("/v1/GET/st/handle/{id}")
    public AjaxResult handle(@PathVariable String id) {
        return AjaxResult.success(service.relStHandle(id));
    }

    @Log(title = "处理器规则", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/st/handle")
    public AjaxResult add(@Validated @RequestBody RelStHandleEntity entity) {
        return toAjax(service.insert(entity));
    }

    /**
     * 修改测站、设备关联处理器配置
     */
    @Log(title = "处理器规则", businessType = BusinessType.UPDATE)
    @PutMapping("/v1/PUT/st/handle")
    public AjaxResult edit(@Validated @RequestBody RelStHandleEntity entity) {
        return toAjax(service.update(entity));
    }

    /**
     * 初始化测站缓存数据到redis中，数据格式（stCode, List<RelStHandleEntity>）,（测站编码，测站关联处理器列表含顺序）
     */
    @Log(title = "初始化配置数据到redis", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/st/handle/cache")
    public AjaxResult initCache() {
        service.initCache();
        return AjaxResult.success();
    }

    /**
     * 删除测站、设备关联处理器配置
     */
    @Log(title = "测站、设备关联处理器配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/v1/DELETE/st/handle/{ids}")
    public AjaxResult remove(@PathVariable long[] ids) {
        service.delete(ids);
        return success();
    }

}
